<?php
namespace App\Admin\Controllers;

use App\Admin\Actions\Post\FreezeUser;
use App\Admin\Exports\MsgLogExport;
use App\Admin\Exports\UserProfileExport;
use App\Admin\Extensions\RoleOp;
use App\Admin\Extensions\UserOp;
use App\Http\Controllers\Controller;
use App\Model\AppChannel;
use App\Model\Guild\GuildHost;
use App\Model\HostBlacklist;
use App\Model\IpBlacklist;
use App\Model\Log\AdminInviteLog;
use App\Model\Message;
use App\Model\User;
use App\Model\UserActLog;
use App\Model\UserDevice;
use App\Model\UserForbidden;
use App\Model\UserFreezeLog;
use App\Model\UserMsgLog;
use App\Model\UserMsgLogTmp;
use App\Model\UserProfile;
use App\Util\ComUtil;
use App\Util\DateUtil;
use App\Util\ImUtil;
use App\Util\InfoUtil;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Model\UsersRechargeLimit;

class  UsersRechargeLimitController extends Controller
{
    use ModelForm;
    public function index()
    {
        $content = new Content();
        $content->header('充值限制');
        $content->body($this->grid());
        return $content;
    }

    protected function grid()
    {
        $grid = new Grid(new UsersRechargeLimit());
        $grid->id('消息ID');
        $grid->fromProfile()->nick_name('用户昵称');
        $grid->fromProfile()->user_profile_id('用户ID')->display(function ($data){
            $str = '内部ID：'.$this->user_id."<br/>";
            $str .='外部ID：'.$this->fromProfile->user_profile_id.'<br/>';
            return $str;
        });
        $grid->recharge_rmb('限制充值金额(当日)');
        $grid->total_recharge_rmb('限制充值(总额)');
        $grid->flag('状态')->display(function ($data){
            return $data == 1 ? '有效' : '无效';
        });
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1/3 , function ($filter) {
                $filter->equal('fromProfile.user_id', '内部ID');
            });
            $filter->column(1/3 , function ($filter) {
                $filter->equal('fromProfile.user_profile_id', '外部ID');

            });
        });
        $grid->disableExport();
        $grid->actions(function (Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
        });
        return $grid;
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('提现金额限制详情');
            $content->description('');
            $content->body($this->form('edit', $id)->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加提现金额限制');
            $content->description('');
            $content->body($this->form());
        });
    }

    /**
     * Make a form builder.
     * @return Form
     */
    protected function form($mode = '', $id = '')
    {
        return Admin::form(UsersRechargeLimit::class, function (Form $form) use ($mode, $id) {
            $form->text('user_id','用户内部ID')
                ->creationRules(['required', "unique:users_recharge_limit"])
                ->updateRules(['required', "unique:users_recharge_limit,user_id,{{id}}"]);
            $form->text('recharge_rmb', '限制金额（每日）');
            $form->hidden('admin_id', '限制金额（每日）');
            $form->text('total_recharge_rmb', '充值总金额')->column();
            $form->select('flag', '是否有效')->options([1 => '有效', 0 => '无效'])->default(1);
            $form->saving(function (Form $form) use($id) {
                $userId = User::where('id',$form->user_id)->first();
                if(!$userId){
                    return InfoUtil::adminErr("输入用户ID有误");
                }
                $form->admin_id = Admin::user()->id;
            });

            $form->tools(function (Form\Tools $tools) {
                // 去掉`删除`按钮
                $tools->disableDelete();
                // 去掉`查看`按钮
                $tools->disableView();
            });
        });
    }
}
